package com.gzceb.zhgjj.security.dao;

// default package

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gzceb.zhgjj.security.entity.SysUsersRoles;

/**
 * A data access object (DAO) providing persistence and search support for
 * SysUsersRoles entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see .SysUsersRoles
 * @author MyEclipse Persistence Tools
 */

public class SysUsersRolesDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(SysUsersRolesDAO.class);
	// property constants
	public static final String ENABLED = "enabled";

	protected void initDao() {
		// do nothing
	}

	public void persist(SysUsersRoles transientInstance) {
		log.debug("persisting SysUsersRoles instance");
		try {
			getHibernateTemplate().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void save(SysUsersRoles transientInstance) {
		log.debug("saving SysUsersRoles instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(SysUsersRoles persistentInstance) {
		log.debug("deleting SysUsersRoles instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SysUsersRoles findById(java.lang.Long id) {
		log.debug("getting SysUsersRoles instance with id: " + id);
		try {
			SysUsersRoles instance = (SysUsersRoles) getHibernateTemplate()
					.get("SysUsersRoles", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(SysUsersRoles instance) {
		log.debug("finding SysUsersRoles instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SysUsersRoles instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from SysUsersRoles as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByEnabled(Object enabled) {
		return findByProperty(ENABLED, enabled);
	}

	public List findAll() {
		log.debug("finding all SysUsersRoles instances");
		try {
			String queryString = "from SysUsersRoles";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/**
	 * 根据角色id返回用户和角色的关联关系的实例列表。
	 * 
	 * @author liqing
	 * @param roleId
	 * @return SysUsersRoles列表
	 */
	public List<SysUsersRoles> getUsersRolesLst(String roleId) {
		log.debug("finding SysUsersRoles list by roleId");
		try {
			List<SysUsersRoles> list = getHibernateTemplate().find(
					"from SysUsersRoles where role_id='" + roleId + "'");

			return list;

		} catch (RuntimeException re) {
			log.error("find SysUsersRoles list failed.");
			throw re;
		}
	}

	public SysUsersRoles merge(SysUsersRoles detachedInstance) {
		log.debug("merging SysUsersRoles instance");
		try {
			SysUsersRoles result = (SysUsersRoles) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(SysUsersRoles instance) {
		log.debug("attaching dirty SysUsersRoles instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SysUsersRoles instance) {
		log.debug("attaching clean SysUsersRoles instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static SysUsersRolesDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (SysUsersRolesDAO) ctx.getBean("SysUsersRolesDAO");
	}
}